package com.example.administrator.szb.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.administrator.szb.bean.UserBase;
import com.example.administrator.szb.bean.UserInfo2;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;


/**
 * SharedPreferences工具类
 */
public class SPUtils
{
	private static final String TAG = "SPUtils";
	public static final String USER_INFO = "userInfo";
	public static final String USER_BASE = "user_base";
	public static final String USERS_INTO = "users_info";
	public static final String USER_ID = "user_id";
	public static final String USER_PHONE = "user_phone";
	public static final String SHOW_MSG = "show_msg";
	public static final String JPUSH_TAG = "jpush_tag";
	public static final String IS_LOADED_YDY = "is_loaded_ydy";
	private static Context context = SampleApplicationLike.getInstance();

	public static boolean jpushSuccess(){
		String str = getDatasString(JPUSH_TAG);
		if (TextUtils.isEmpty(str)){
			return false;
		}
		return true;
	}


	public static boolean isShowMsg(){
		String str = getDatasString(SHOW_MSG);
		if (str==null||str.equals("no")){
			return false;
		}
		return true;
	}

	/**
	 * 判断是否登录
	 * @param
	 * @return
     */
	public static boolean isLogin() {
		SharedPreferences sp = context.getSharedPreferences(USER_INFO, context.MODE_PRIVATE);

		String userSeq = sp.getString(USER_BASE, null);

		if (userSeq == null)
			return false;

		return true;
	}
	public static UserInfo2 getUser(){
		SharedPreferences sp = context.getSharedPreferences(USER_INFO, context.MODE_PRIVATE);
		String userSeq = sp.getString(USERS_INTO, null);
		if (userSeq == null){
			return null;
		}
		UserInfo2 userInfo = JSON.parseObject(userSeq,new TypeReference<UserInfo2>(){});
		return userInfo;
	}


	/**
	 * 获取保存的信息（String)
     */
	public static UserBase getUserinfo() {
		SharedPreferences sp = context.getSharedPreferences(USER_INFO, context.MODE_PRIVATE);
		String userSeq = sp.getString(USER_BASE, null);
		if (userSeq == null){
			return null;
		}
		UserBase userInfo = JSON.parseObject(userSeq,new TypeReference<UserBase>(){});
		return userInfo;
	}

	/**
	 * 获取保存的信息（String)
	 */
	public static int getUserId() {
		SharedPreferences sp = context.getSharedPreferences(USER_INFO, context.MODE_PRIVATE);
		int userSeq = sp.getInt(USER_ID, -1);

		if (userSeq == -1){
			return 0;
		}
		return userSeq;
	}

	/**
	 * 获取保存的信息(int)
	 * @param
	 * @param key
	 * @return
	 */
	public static int getDatasInt(String key) {
		SharedPreferences sp = context.getSharedPreferences(USER_INFO, context.MODE_PRIVATE);

		return sp.getInt(key,-1);
	}
	/**
	 * 获取保存的信息(String)
	 * @param
	 * @param key
	 * @return
	 */
	public static String getDatasString(String key) {
		SharedPreferences sp = context.getSharedPreferences(USER_INFO, context.MODE_PRIVATE);

		return sp.getString(key,null);
	}
	/**
	 * 获取保存的信息(Long)
	 * @param
	 * @param key
	 * @return
	 */
	public static long getDatasLong(String key) {
		SharedPreferences sp = context.getSharedPreferences(USER_INFO, context.MODE_PRIVATE);

		return sp.getLong(key,-1L);
	}
	/**
	 * 保存信息
	 * @param
	 * @param key
	 * @param value
     */
	public static void setDatas(String key, Object value){
		SharedPreferences sp = context.getSharedPreferences(USER_INFO, context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		if (value instanceof String)
		editor.putString(key,(String)value);
		else if (value instanceof Integer)
			editor.putInt(key,(int)value);
		else if (value instanceof Long)
			editor.putLong(key,(long)value);
		editor.commit();

	}

	/**
	 * 清空
	 * @param
     */
	public static void clear(){

		SharedPreferences sp = context.getSharedPreferences(USER_INFO, context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		editor.clear();
		editor.commit();
	}



}